//
//  HWEveryDayTaskController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/6/21.
//  Copyright © 2022 94hwan. All rights reserved.
//

import UIKit
import JXPagingView
class HWEveryDayTaskController: HWBaseViewController {
    
    var tableViewBgView:UIView!
    var tableView:UITableView!
    var tableViewProtocol = HWEveryDayTaskProtocol()
    var httpManager = HWTaskTakeMoneyHttpManager()
    
    var listViewDidScrollCallback: ((UIScrollView) -> ())?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
        //        loadData()
        
        addAction()
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
}

extension HWEveryDayTaskController{
    private func initUI() -> Void {
        navigationView.isHidden = true
        view.backgroundColor = .clear
        
        
        tableViewBgView = UIView()
        tableViewBgView.backgroundColor = .white
        tableViewBgView.layer.cornerRadius = 14*widthScale
        tableViewBgView.frame = CGRect(x: 0, y: 0, width: 339*widthScale, height: 473*widthScale)
        view.addSubview(tableViewBgView)
        
        tableViewBgView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#E7E7E7", alpha: 0.42).cgColor
        tableViewBgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        tableViewBgView.layer.shadowRadius = 9*widthScale
        tableViewBgView.layer.shadowOpacity = 1
        
        
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: 339*widthScale, height: 473*widthScale), style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .white
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
        
        tableView.layer.cornerRadius = 14*widthScale
        
        
    }
    
    private func initFrame() -> Void {
        
        tableViewBgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(10*widthScale)
            make.bottom.equalTo(-18)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(10*widthScale)
            make.bottom.equalTo(-18)
        }
        
    }
}
extension HWEveryDayTaskController:JXPagingViewListViewDelegate {
    func listView() -> UIView {
        return view
    }
    
    func listScrollView() -> UIScrollView {
        return self.tableView
    }
    
    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
        self.listViewDidScrollCallback = callback
    }
}

extension HWEveryDayTaskController {
    func loadData() -> Void {
        httpManager.getEveryDayTaskList { model in
            self.tableViewProtocol.model = model
            self.tableView.reloadData()
        } failure: { errorString in
            
        }
        
    }
    
    func everyDayReceive(_ type:String) -> Void {
        httpManager.getEveryDayTaskReceive(type: type) { success in
            self.view.showHUD(title: success)
            self.loadData()
        } failure: { errorString in
            
        }
        
    }
}

extension HWEveryDayTaskController {
    func addAction() -> Void {
        tableViewProtocol.scrollDidScroll = {
            scrollView in
            self.listViewDidScrollCallback?(scrollView)
        }
        self.tableViewProtocol.didClickRightButton = {
            model in
            if HWLocallyStoredData.getLoginUserID().count > 0 {
                if model.status == "2"{
                    
                }else if model.status == "1" {
                    //领取
                    self.everyDayReceive(model.type)
                }else {
                    if model.type == "12"  {
                        self.navigationController?.pushViewController(HWDailyCheckController(), animated: true)
                    } else if model.type == "2" {
                        let webVC = XMGWebViewController()
                        let tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
                        let uid = HWLocallyStoredData.getLoginUserID()
                        let sign = "\(uid)\(tStr)\("fsd213ewdsadqwe2121213edsad")"
                        let signMD5 = WMUtil.md5HexDigest(sign) ?? ""
                        webVC.url = URL(string: "\(HWGlobalConst.shared.baseUrl)?ct=choujiang&uid=\(uid)&t=\(tStr)&sign=\(signMD5)")
                        self.navigationController?.pushViewController(webVC, animated: true)
                    } else if model.type == "3" {
                        HWUniversal.getCurrentVc().navigationController?.pushViewController(HWPaiHangBangViewController(), animated: true)
                    } else if model.type == "4" {
                        /// 每日首冲
                        let webView = XMGWebViewController()
                        webView.isPay = true
                        self.navigationController?.pushViewController(webView, animated: true)
                    } else if model.type == "10" {
                        /// 邀请好友
                        self.navigationController?.pushViewController(HWInviteFriendsViewController(), animated: true)
                    } else if model.type == "5" {
                        ///设置头像
                        self.navigationController?.pushViewController(HWPersonalDataViewController(), animated: true)
                    } else if model.type == "6" {
                        /// 设置昵称
                        self.navigationController?.pushViewController(HWPersonalDataViewController(), animated: true)
                    } else if model.type == "7" {
                        /// 完善信息
                        self.navigationController?.pushViewController(HWPersonalDataViewController(), animated: true)
                    } else if model.type == "8" {
                        /// 实名认证
                        self.navigationController?.pushViewController(HWAccountSecurityController(), animated: true)
                    } else if model.type == "9" {
                        /// 绑定手机
                        self.navigationController?.pushViewController(HWAccountSecurityController(), animated: true)
                    }
                }
            } else {
                self.navigationController?.pushViewController(HWLoginController(), animated: true)
            }
            
            
        } 
    }
}
